package streaming;

import java.util.Vector;

import javax.media.CaptureDeviceInfo;
import javax.media.CaptureDeviceManager;
import javax.media.format.AudioFormat;
import javax.media.format.VideoFormat;
import javax.swing.ComboBoxModel;
import javax.swing.event.ListDataListener;

import jmapps.ui.PopupWait;
import parameters.RLM_Parameters;

public class StreamingConfigurer {

	private Vector<CaptureDeviceInfo> audioDevices;
	private Vector<CaptureDeviceInfo> videoDevices;

	private Vector<String> audioDeviceNames = new Vector<String>();
	private Vector<String> videoDeviceNames = new Vector<String>();

	private String selectedAudioDevice = null;
	private String selectedVideoDevice = null;

	private String selectedAudioOld = null;
	private String selectedVideoOld = null;

	public StreamingConfigurer() {
		selectedAudioDevice = RLM_Parameters.getAudioDevice();
		selectedVideoDevice = RLM_Parameters.getVideoDevice();
		refreshDevices();
	}

	public void save(){
		RLM_Parameters.setAudioDevice(selectedAudioDevice);
		RLM_Parameters.setVideoDevice(selectedVideoDevice);
		RLM_Parameters.storeParameters();
	}



	public void refreshDevices(){
		audioDevices = CaptureDeviceManager.getDeviceList(new AudioFormat(null));
		videoDevices = CaptureDeviceManager.getDeviceList(new VideoFormat(null));

		audioDeviceNames.removeAllElements();
		for(CaptureDeviceInfo device : audioDevices){
			audioDeviceNames.add(device.getName());

		}

		if(selectedAudioDevice == null || selectedAudioDevice.equals("") || !audioDeviceNames.contains(selectedAudioDevice)){
			selectedAudioDevice = audioDeviceNames.size()!=0?audioDeviceNames.get(0):"";
		}

		videoDeviceNames.removeAllElements();
		for(CaptureDeviceInfo device : videoDevices){
			videoDeviceNames.add(device.getName());
		}

		if(selectedVideoDevice == null 
				|| selectedVideoDevice.equals("") 
				|| !videoDeviceNames.contains(selectedVideoDevice)){
			selectedVideoDevice = videoDeviceNames.size()!=0?videoDeviceNames.get(0):"";
		}
	}



	public Vector<CaptureDeviceInfo> getAudioDevices() {
		return audioDevices;
	}


	public void setAudioDevices(Vector<CaptureDeviceInfo> audioDevices) {
		this.audioDevices = audioDevices;
	}


	public Vector<CaptureDeviceInfo> getVideoDevices() {
		return videoDevices;
	}


	public void setVideoDevices(Vector<CaptureDeviceInfo> videoDevices) {
		this.videoDevices = videoDevices;
	}

	public String getSelectedAudioDevice() {
		return selectedAudioDevice;
	}

	public void setSelectedAudioDevice(String selectedAudioDevice) {
		if(this.selectedAudioOld == null){
			this.selectedAudioOld = this.selectedAudioDevice;
		}
		this.selectedAudioDevice = selectedAudioDevice;
	}

	public void setSelectedAudioDevice(int i) {
		if(this.selectedAudioOld == null){
			this.selectedAudioOld = this.selectedAudioDevice;
		}
		this.selectedAudioDevice = audioDeviceNames.get(i);
	}

	public String getSelectedVideoDevice() {
		return selectedVideoDevice;
	}

	public void setSelectedVideoDevice(String selectedVideoDevice) {
		if(this.selectedVideoOld == null){
			this.selectedVideoOld = this.selectedVideoDevice;
		}
		this.selectedVideoDevice = selectedVideoDevice;
	}

	public void setSelectedVideoDevice(int i) {
		if(this.selectedVideoOld == null){
			this.selectedVideoOld = this.selectedVideoDevice;
		}
		this.selectedVideoDevice = videoDeviceNames.get(i);
	}

	public Vector<String> getVideoDeviceNames() {
		return videoDeviceNames;
	}

	public void setVideoDeviceNames(Vector<String> videoDeviceNames) {
		this.videoDeviceNames = videoDeviceNames;
	}

	public Vector<String> getAudioDeviceNames() {
		return audioDeviceNames;
	}

	public void setAudioDeviceNames(Vector<String> audioDeviceNames) {
		this.audioDeviceNames = audioDeviceNames;
	}

	public String getSelsectedOld() {
		return selectedAudioOld;
	}

	public void setSelsectedOld(String selsectedOld) {
		this.selectedAudioOld = selsectedOld;
	}

	public String getSelectedVideoOld() {
		return selectedVideoOld;
	}

	public void setSelectedVideoOld(String selectedVideoOld) {
		this.selectedVideoOld = selectedVideoOld;
	}

	public void revertChanges() {
		if(this.selectedAudioOld != null){
			this.selectedAudioDevice = this.selectedAudioOld;
			selectedAudioOld = null;
		}
		if(this.selectedVideoOld != null){
			this.selectedVideoDevice = this.selectedVideoOld;
			selectedVideoOld = null;
		}
	}

	public void detectDevices () {
		// Check if VFWAuto or SunVideoAuto is available
		Class       directAudio = null;
		Class       autoAudio = null;
		Class       autoVideo = null;
		Class       autoVideoPlus = null;
		Object      instanceAudio;
		Object      instanceVideo;
		Object      instanceVideoPlus;
		PopupWait   popupWait;

		try {
			directAudio = Class.forName("DirectSoundAuto");
		}
		catch (Exception e) {
		}

		try {
			autoAudio = Class.forName("JavaSoundAuto");
		}
		catch (Exception e) {
		}

		try {
			autoVideo = Class.forName("VFWAuto");

		}
		catch (Exception e) {
		}

		if (autoVideo == null) {
			try {
				autoVideo = Class.forName("SunVideoAuto");
			}
			catch (Exception ee) {
			}
			try {
				autoVideoPlus = Class.forName("SunVideoPlusAuto");
			}
			catch (Exception ee) {
			}
		}
		if (autoVideo == null) {
			try {
				autoVideo = Class.forName("V4LAuto");
			} catch (Exception eee) {
			}
		}

		if ( directAudio == null && autoAudio == null  &&
				autoVideo == null  &&  autoVideoPlus == null )
			return;

		try {
			if ( directAudio != null ){
				instanceAudio = directAudio.newInstance ();
			}
			if ( autoAudio != null ){
				instanceAudio = autoAudio.newInstance ();
			}
			if ( autoVideo != null ){
				instanceVideo = autoVideo.newInstance ();
			}
			if (autoVideoPlus != null){
				instanceVideoPlus = autoVideoPlus.newInstance ();
			}
		}
		catch ( ThreadDeath td ) {
			throw td;
		}
		catch (Throwable t) {
		}
	}

	public class AudioComboBoxModel implements ComboBoxModel{

		@Override
		public Object getSelectedItem() {
			return selectedAudioDevice;
		}

		@Override
		public void setSelectedItem(Object anItem) {
			selectedAudioDevice = (String)anItem;
		}

		@Override
		public void addListDataListener(ListDataListener l) {

		}

		@Override
		public Object getElementAt(int index) {
			return audioDeviceNames.get(index);
		}

		@Override
		public int getSize() {
			return audioDeviceNames.size();
		}

		@Override
		public void removeListDataListener(ListDataListener l) {

		}

	}
	public class VideoComboBoxModel implements ComboBoxModel{

		@Override
		public Object getSelectedItem() {
			return selectedVideoDevice;
		}

		@Override
		public void setSelectedItem(Object anItem) {
			selectedVideoDevice = (String)anItem;
		}

		@Override
		public void addListDataListener(ListDataListener l) {

		}

		@Override
		public Object getElementAt(int index) {
			return videoDeviceNames.get(index);
		}

		@Override
		public int getSize() {
			return videoDeviceNames.size();
		}

		@Override
		public void removeListDataListener(ListDataListener l) {

		}

	}




}
